<template>
  <div class="app-container home">
    <el-tabs type="border-card" v-model="activeTab" @tab-click="switchTab">
      <el-tab-pane v-if="privilegeList.includes('GYS')" name="1" label="门市管理"/>
      <el-tab-pane v-if="privilegeList.includes('MS')" name="2" label="供应商管理"/>
      <el-form v-if="activeTab==='1'||activeTab==='2'" :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
        <el-form-item :label="activeTab==='1'?'门市名称':'供应商名称'" prop="name">
          <el-input v-model="queryParams.name" placeholder="" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div v-loading="loading" class="table-list">
        <template v-if="activeTab === '1'">
          <StoreCard v-for="item in tableData" :item="item" :id="item.id"/>
        </template>
        <template v-else-if="activeTab === '2'">
          <SupplierCard v-for="item in tableData" :item="item" :id="item.id"/>
        </template>
        <template v-else>

        </template>
      </div>
      <pagination v-show="total>0" :total="total"
                  :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </el-tabs>
  </div>
</template>

<script>
import {storePageList, supplierPageList} from "@/api";
import StoreCard from './components/storeCard'
import SupplierCard from './components/supplierCard'

export default {
  name: "index",
  components: {StoreCard, SupplierCard},
  data() {
    return {
      activeTab: '1',
      loading: false,
      queryParams: {
        name: '',
        pageNum: 1,
        pageSize: 10,
        isIndex: 1
      },
      privilegeList:[],
      total: 0,
      tableData: []
    };
  },
  computed: {
    tabNumber() {
      return Number(this.activeTab)
    }
  },
  created() {
    this.privilegeList=this.$store.state.user.userInfo.privilegeList;
    if (this.privilegeList.includes('MS')){
      this.activeTab='2';
    }else if (this.privilegeList.includes('GYS')){
      this.activeTab='1';
    }else{
      this.activeTab='3';
      return;
    }
    this.getList();
  },
  methods: {
    switchTab() {
      this.queryParams.name = '';
      this.handleQuery()
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      let methodsList = {
        1: storePageList,
        2: supplierPageList,
      }
      this.loading = true
      methodsList[this.tabNumber]({
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        isIndex:1,
        [this.activeTab === '1' ? 'storeName' : 'supplierName']: this.queryParams.name
      }).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
      }).finally(() => {
        this.loading = false;
      })
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
  }
};
</script>

